﻿/****************************** 模块头 ******************************\
* 模块名:  SysConfigManager.cs
* 项目名:  DingTalkAPIHelp
* 版权(c)  auth markeluo.
* 
*说明： SysConfigManager 用于配置钉钉接口调用相关的配置、token管理
* This source is subject to the markeluo
* All other rights reserved.
\***************************************************************************/

using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DingTalkAPIHelp.Base
{
    public class SysConfigManager
    {
        /// <summary>
        /// 刷新的token信息
        /// </summary>
        private string _accessToken = "";
        private string _Corpid = "ding5752423f348a9e8935c2f4657eb6378f";//企业凭证id
        private string _Corpsecret = "FMoDrdXkEHAppj8aY7jVF5CVfFyYE95wkEdgF8OykUxnweHYduhrRQeD8ybBqFFj";//企业凭证密钥
        private DateTime ExpiresDate = DateTime.Now;

        /// <summary>
        /// 应用授权处理
        /// </summary>
        private string _appAccessToken = "";
        private long _AgentId = 1267980550;//应用agentid
        private DateTime appTokenExpiresDate = DateTime.Now;
        private string _appKey = "dingphkbmn7tmq28bvlo";//AppKey
        private string _appSecret = "-SsnOBpHijgNjp6qPv_4I8HnrWFX2mZ9rrJx07jpKUmrMSgQlEamL0PcfbeP7zjU";//AppSecret

        /// <summary>
        /// 应用agentid
        /// </summary>
        public long AgentId
        {
            get { return _AgentId; }
            set { _AgentId = value; }
        }
        /// <summary>
        /// token信息获取
        /// </summary>
        public string accessToken
        {
            get
            {
                Get_Token();//更新token
                return _accessToken;
            }
        }

        /// <summary>
        /// APP 应用Token获取
        /// </summary>
        public string appAccessToken
        {
            get
            {
                get_AppToken();
                return _appAccessToken;
            }
        }

        /// <summary>
        /// 1.获取Token
        /// </summary>
        private void Get_Token()
        {
            if (ExpiresDate > DateTime.Now)
            {
                //判断是否过期
                return;
            }
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.Corpid = _Corpid;
            req.Corpsecret = _Corpsecret;
            req.SetHttpMethod("GET");
            OapiGettokenResponse response = client.Execute(req);
            if (!string.IsNullOrWhiteSpace(response.AccessToken)){
                ExpiresDate = DateTime.Now.AddSeconds(response.ExpiresIn);
                _accessToken = response.AccessToken;
            }
        }

        /// <summary>
        /// 获取AppToken
        /// </summary>
        private void get_AppToken()
        {
            if (appTokenExpiresDate > DateTime.Now)
            {
                //判断是否过期
                return;
            }

            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest req = new OapiGettokenRequest();
            req.Appkey = _appKey;
            req.Appsecret = _appSecret;
            req.SetHttpMethod("GET");
            OapiGettokenResponse response = client.Execute(req,accessToken);
            if (!string.IsNullOrWhiteSpace(response.AccessToken))
            {
                appTokenExpiresDate = DateTime.Now.AddSeconds(response.ExpiresIn);
                _appAccessToken = response.AccessToken;
            }
        }
        
    }
}
